package com.qzxy.premiumhotelsystem.controller.admin;

import cn.dev33.satoken.annotation.SaIgnore;
import com.qzxy.premiumhotelsystem.common.constant.RoomStatusConstant;
import com.qzxy.premiumhotelsystem.common.result.PageResult;
import com.qzxy.premiumhotelsystem.common.result.Result;
import com.qzxy.premiumhotelsystem.common.result.ResultCodeEnum;
import com.qzxy.premiumhotelsystem.domain.dto.RoomsDTO;
import com.qzxy.premiumhotelsystem.domain.dto.RoomsPageDTO;
import com.qzxy.premiumhotelsystem.domain.entity.Rooms;
import com.qzxy.premiumhotelsystem.service.RoomsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * ClassName: RoomsController
 * Package: com.qzxy.premiumhotelsystem.controller
 * Description:
 *
 * @Author Qwh
 * @Create 2024/9/12 16:03
 * @Version 1.0
 */

@Tag(name = "管理端房间", description = "管理端房间相关接口")
@RestController("adminRoomsController")
@RequestMapping("/admin/rooms")
@Slf4j
public class RoomsController {

    @Resource
    private RoomsService roomsService;

    /**
     * 新增房间
     *
     * @param roomsDTO
     * @return
     */
    @Operation(summary = "新增房间")
    @PostMapping("/add")
    public Result addRoom(@RequestBody @Validated RoomsDTO roomsDTO) {

        Result result = roomsService.addRoom(roomsDTO);
        return result;
    }

    /**
     * 根据房间id查询房间相关信息 管理员或员工来查看使用房间id
     *
     * @param roomId
     * @return
     */
    @Operation(summary = "查询房间", description = "根据房间id查询房间相关信息")
    @GetMapping("/{roomId}")
    public Result selectByRoomId(@PathVariable Integer roomId) {
        Result result = roomsService.selectByRoomId(roomId);
        return result;
    }


    /**
     * 房间列表分页查询
     *
     * @param roomsPageDTO
     * @return
     */
    @Operation(summary = "房间列表分页查询")
    @GetMapping("/list")
    public Result<PageResult> list(@ParameterObject @Validated RoomsPageDTO roomsPageDTO) {

        Result<PageResult> result = roomsService.pageQuery(roomsPageDTO);
        return result;

    }
    /**
     * 房间列表查询
     *
     * @return
     */
    @Operation(summary = "房间列表查询")
    @GetMapping("/all/list")
    @SaIgnore
    public Result allList() {
        return Result.ok(roomsService.allList());
    }

    /**
     * 修改房间
     *
     * @param rooms
     * @return
     */
    @Operation(summary = "根据id修改房间信息",description = "这里若是要修改房间状态 status填字符串 空闲 已预订 已入住 维修中")
    @PutMapping("/update")
    public Result updateRoom(@RequestBody @Validated Rooms rooms) {
        boolean updated = roomsService.updateById(rooms);
        if (!updated) {
            return Result.build("更新失败,房间不存在", ResultCodeEnum.SERVER_ERROR);
        }
        return Result.ok("更新房间成功");
    }

    /**
     * 根据id删除房间
     *
     * @param roomId
     * @return
     */
    @Operation(summary = "根据id删除房间")
    @DeleteMapping("/{roomId}")
    public Result deleteRooms(@PathVariable Integer roomId) {
        boolean removed = roomsService.removeById(roomId);
        if (!removed) {
            return Result.build("删除失败，房间不存在", ResultCodeEnum.SERVER_ERROR);
        }
        return Result.ok("删除成功");
    }

    /**
     * 根据房间的号码修改状态
     */
    @Operation(summary = "根据房间号修改房间状态",description = "房间状态填数字 1-空闲 2-已预订、3-已入住、4-维修中")
    @PutMapping("/status/update/{roomNumber}/{statusNumber}")
    @SaIgnore
    public Result updateRoomStatusByRoomNumber(@PathVariable String roomNumber, @PathVariable Integer statusNumber) {

        String status = RoomStatusConstant.getStatusByNumber(statusNumber);
        if (status == null) {
            throw new RuntimeException("无效的房间状态编号");
        }
        log.info("开始修改房间号{}的状态为{}",roomNumber,status);
        roomsService.updateRoomStatusByRoomNumber(roomNumber, status);
        return Result.ok("房间状态更新成功");
    }


    @Operation(summary = "房间状态种类查询，便于前端通过status关键字来查找房间")
    @GetMapping("/status/all")
    public Result listRoomStatus() {
        log.info("用户查询房间");
        Result result = roomsService.getRoomStatusList();
        return result;
    }





}
